//----------------------------------
//------PSP UMD Launcher------------
//------------Made by Luafox--------
//----------------------------------
//---Part of the SMOD Weapons Pack--
//----------------------------------

if ( SERVER ) then
	AddCSLuaFile( "cl_init.lua" )	
	AddCSLuaFile( "shared.lua" )
	SWEP.HoldType			= "grenade"
end

if ( CLIENT ) then
	--Settings
	SWEP.Author		= "Luafox"
	SWEP.PrintName 		= "UMD Launcher"
	SWEP.Slot			= 3
	SWEP.SlotPos		= 3
	SWEP.DrawAmmo 		= true
	SWEP.DrawCrosshair	= false
	SWEP.Category			= "SMOD"
	SWEP.WepSelectIcon = surface.GetTextureID("weapons/pspicon")

	-- Kill Icon
	if(file.Exists("../materials/weapons/psp_killicon.vmt")) then
		killicon.Add("smod_umd","weapons/psp_killicon",Color(255,255,255))
	end 

end
SWEP.ViewModel		= "models/weapons/v_psp.mdl"
SWEP.WorldModel		= "models/weapons/w_psp.mdl"
SWEP.Weight		= 5
SWEP.AutoSwitchTo		= true
SWEP.AutoSwitchFrom		= false
SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true
--Info
SWEP.Contact		= "Maybe later"
SWEP.Purpose		= "Use it instead of a real PSP"
SWEP.Instructions	= "Shoot UMDs"

--Variables--
SWEP.PrimeroDelay = CurTime()
SWEP.SeconderoDelay = CurTime()

function SWEP:Initialize()
end

function SWEP:Precache()
util.PrecacheSound("weapons/psp/spin.wav")
util.PrecacheSound("weapons/psp/reload.wav")

end

function SWEP:PrimaryAttack()
	if self.PrimeroDelay < CurTime() then
		self.PrimeroDelay = CurTime()+ 2.3
		local Anims =
				{
					ACT_VM_PRIMARYATTACK_1,
					ACT_VM_PRIMARYATTACK_2,
					ACT_VM_PRIMARYATTACK_3,
					ACT_VM_PRIMARYATTACK_4
				}

			self:SendWeaponAnim(Anims[math.random(1,4)]); -- randomize the animations

		timer.Simple(0.21, self.UMDs,self);
		

	end
end

function SWEP:UMDs() -- self made function for umd spawning
		local Front = self.Owner:GetAimVector();
		local Up = self.Owner:EyeAngles():Up();
		
		if ( SERVER ) then -- only spawn things on server to prevent issues
		self.Weapon:EmitSound(Sound("weapons/psp/spin.wav")) 
			//effect data
			local FX = EffectData();
			FX:SetOrigin(self.Owner:GetShootPos() + Front * 18.5 + Up * 5 * -1);
			FX:SetNormal(self.Owner:GetAimVector());
			FX:SetMagnitude(1.8);
			FX:SetScale(1);
			FX:SetRadius(5.5);

			//spark effect
			util.Effect("sparks",FX,true,true);
			local umd = ents.Create("smod_umd");

			if ValidEntity(umd) then -- is this entity valid?

				umd:SetPos(self.Owner:GetShootPos() + Front * 10 + Up * 10 * -1);
				umd:SetAngles(Front:Angle());
				umd:Spawn();
				umd:Activate();
				umd:SetOwner(self.Owner) -- now we are the owner to prevent worldspawn getting the frag

				local Physics = umd:GetPhysicsObject();
	
				if ValidEntity(Physics) then
		
				local Random = Front:Angle();
						
						Random = Random:Forward();

						Physics:ApplyForceCenter(Random * 1600);
				end
			end
			timer.Simple(0.66, function() self:SendWeaponAnim(ACT_VM_RELOAD); timer.Simple(0.7, function() self.Weapon:EmitSound(Sound("weapons/psp/reload.wav")); end);  end) -- animationstuff
		end
end 

function SWEP:SecondaryAttack()
	if self.SeconderoDelay < CurTime() then
		self.SeconderoDelay = CurTime()+ 2.975
	self:SendWeaponAnim(ACT_VM_PRIMARYATTACK_5)
	end
end

function SWEP:Think()
end

function SWEP:Reload()
end

//always return true on holster and deploy
function SWEP:Deploy()
	self:SendWeaponAnim(ACT_VM_DRAW);
	return true
end
function SWEP:Holster()
	return true
end

-- Primary things
SWEP.Primary.ClipSize	= -1
SWEP.Primary.DefaultClip	= -1 
SWEP.Primary.Automatic	= true
SWEP.Primary.Ammo	= "none"

-- Secondary things
SWEP.Secondary.ClipSize	= -1
SWEP.Secondary.DefaultClip	= -1 
SWEP.Secondary.Automatic	= true
SWEP.Secondary.Ammo	= "none"
